﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ADShop.Models;
using System.Web.Routing;
namespace ADShop.Areas.Admin.Controllers
{
    public class PromotionsProductController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (Session["Admin"] == null)
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                {
                    controller = "Oops",
                    action = "Index"
                }));
            }
        }
        //private ADShopEntities db = new ADShopEntities();
        private EShopEntities1 db = new EShopEntities1();

        //
        // GET: /Admin/PromotionsProduct/

        public ActionResult Index()
        {
            return View(db.PromotionsProduct.ToList());
        }
        public string GetImages(int id)
        {
            if (!db.ImagesProduct.Any(d => d.IDProduct == id))
            {
                return string.Format("<img src='http://upload.wikimedia.org/wikipedia/commons/a/ac/No_image_available.svg' width='75' height='75' alt='No Images Aviable !' />");
            }
            return string.Format("<img src='{0}' width='75' height='75' alt='{1}' />", db.ImagesProduct.Where(d => d.IDProduct == id).FirstOrDefault().LinkImagesProduct, db.ImagesProduct.Where(d => d.IDProduct == id).FirstOrDefault().DescriptionImages);
        }
        //
        // GET: /Admin/PromotionsProduct/Details/5

        public ActionResult Details(int id = 0)
        {
            PromotionsProduct promotionsproduct = db.PromotionsProduct.Find(id);
            if (promotionsproduct == null)
            {
                return HttpNotFound();
            }
            return View(promotionsproduct);
        }

        //
        // GET: /Admin/PromotionsProduct/Create

        public ActionResult Create()
        {
            ViewBag.IDProduct = new SelectList(db.Product, "IDProduct", "NameProduct", 0);
            return View();
        }

        //
        // POST: /Admin/PromotionsProduct/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(PromotionsProduct promotionsproduct)
        {
            if (ModelState.IsValid)
            {
                db.PromotionsProduct.Add(promotionsproduct);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDProduct = new SelectList(db.Product, "IDProduct", "NameProduct", promotionsproduct.IDPromotions);
            return View(promotionsproduct);
        }

        //
        // GET: /Admin/PromotionsProduct/Edit/5

        public ActionResult Edit(int id = 0)
        {
            PromotionsProduct promotionsproduct = db.PromotionsProduct.Find(id);
            if (promotionsproduct == null)
            {
                return HttpNotFound();
            }
            ViewBag.IDProduct = new SelectList(db.Product, "IDProduct", "NameProduct", promotionsproduct.IDPromotions);

            return View(promotionsproduct);
        }
           
            

        //
        // POST: /Admin/PromotionsProduct/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(PromotionsProduct promotionsproduct)
        {
            if (ModelState.IsValid)
            {
                db.Entry(promotionsproduct).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDProduct = new SelectList(db.Product, "IDProduct", "NameProduct", promotionsproduct.IDPromotions);
            return View(promotionsproduct);
        }

        //
        // GET: /Admin/PromotionsProduct/Delete/5

        public ActionResult Delete(int id = 0)
        {
            PromotionsProduct promotionsproduct = db.PromotionsProduct.Find(id);
            if (promotionsproduct == null)
            {
                return HttpNotFound();
            }
            return View(promotionsproduct);
        }

        //
        // POST: /Admin/PromotionsProduct/Delete/5

  
        public ActionResult DeleteConfirmed(int id)
        {
            PromotionsProduct promotionsproduct = db.PromotionsProduct.SingleOrDefault(d => d.IDPromotions == id);
            db.PromotionsProduct.Remove(promotionsproduct);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}